import CityApi from '../api/city';

export const city = {
    state: {
        cities:[],
        citiesLoadStatus:0,

        city:{},
        cityLoadStatus:0,
    },
    actions: {
        loadCities({commit}){
            commit('setCitiesLoadStatus',1);
            CityApi.getCities().then(function (response) {
                commit('setCitiesLoadStatus',2);
                commit('setCities',response.data.data);
            }).catch(function (error) {
                commit('setCities',[]);
                commit('setCitiesLoadStatus',3);
            });
        },
        loadCity({commit},data){
            commit('setCityLoadStatus',1);
            CityApi.getCity(data.slug).then(function (response) {
                commit('setCityLoadStatus',2);
                commit('setCity',response.data.data);
            }).catch(function (error) {
                commit('setCityLoadStatus',3);
                commit('setCity',{});
            })
        }
    },
    mutations: {
        setCities(state,cities){
            state.cities = cities;
        },
        setCitiesLoadStatus(state,status){
            state.citiesLoadStatus = status;
        }
    },
    getters: {
        getCities(state){
            return state.cities;
        },
        getCitiesLoadStatus(state){
            return state.citiesLoadStatus;
        },

        getCity(state){
            return state.city;
        },
        getCityLoadStatus(state){
            return state.cityLoadStatus;
        }

    }
};
